using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using System.Linq;

public class UIEditor : OdinMenuEditorWindow
{

    protected override OdinMenuTree BuildMenuTree()
    {
        var tree = new OdinMenuTree();
        tree.Config.DrawSearchToolbar = true;

        // tree.Add("UI配置", new UIConfig());
        tree.AddAllAssetsAtPath("UI", EditorPathMgr.Instance.UIEditorCfgPath, typeof(UIEditorItem), true, true);
        tree.AddAllAssetsAtPath("Test", "Assets/Scripts/Core/Editor/Config/Test", typeof(TestShow), true, true);

        return tree;
    }
    protected override void OnBeginDrawEditors()
    {
        var selected = this.MenuTree.Selection.FirstOrDefault();
        var toolbarHeight = this.MenuTree.Config.SearchToolbarHeight;

        // Draws a toolbar with the name of the currently selected menu item.
        SirenixEditorGUI.BeginHorizontalToolbar(toolbarHeight);
        {
            if (selected != null)
            {
                //get selected toggle
                GUILayout.Label(selected.Name);
            }

            if (SirenixEditorGUI.ToolbarButton(new GUIContent("Export UIConfig")))
            {
                var filePath = EditorPathMgr.Instance.GameUICfgAsset;
                var config = AssetDatabase.LoadAssetAtPath<UIConfig>(filePath);
                if (config == null)
                {
                    config = ScriptableObject.CreateInstance<UIConfig>();
                    AssetDatabase.CreateAsset(config, filePath);
                }
                if (config.Dic == null)
                {
                    config.Dic = new List<UIItemCfg>();
                }
                config.Dic.Clear();
                // search all asset
                var guids = AssetDatabase.FindAssets("t:UIEditorItem", new string[] { EditorPathMgr.Instance.UIEditorCfgPath });
                foreach (var guid in guids)
                {
                    var path = AssetDatabase.GUIDToAssetPath(guid);
                    var data = AssetDatabase.LoadAssetAtPath<UIEditorItem>(path);
                    if (data != null)
                    {
                        var item = new UIItemCfg();
                        item.Name = data.Name;
                        var prefab = AssetDatabase.GetAssetPath(data.Prefab);
                        item.Prefab = prefab;
                        item.Layer = data.Layer;
                        item.FullScreen = data.FullScreen;
                        config.Dic.Add(item);
                    }
                }

            }

            if (SirenixEditorGUI.ToolbarButton(new GUIContent("Create Character")))
            {
                // create asset
                // var asset = ScriptableObject.CreateInstance<TestShow>();
                // AssetDatabase.CreateAsset(asset, "Assets/Scripts/Editor/Config/Test/Test.asset");
                // Validator.Instance.WriteUnityType();

            }
        }
        SirenixEditorGUI.EndHorizontalToolbar();
    }
}

